package algorithm;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 二分查找
 */
public class BinarySearch {
    public static void main(String[] args) {
        CreatArr c = new CreatArr();
        int[] arr = c.creatArr(10);
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入想查找的数");
        int num = sc.nextInt();
        Arrays.sort(arr);
        int top =0;
        int wei = arr.length-1;

        while (top<wei){

            int z = (wei+top)/2;
            if (arr[z]>num){
                wei =z-1;
            }else if (arr[z]<num){
                top =z+1;
            }else {
                System.out.println("找到"+num+"啦，下标为"+(z-1));
                break;
            }
        }

    }
}
